//! # 监控配置模块
//! 
//! 定义监控相关的配置结构，包括指标、追踪、日志和健康检查。

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use crate::config::{MetricsConfig, TracingConfig, LoggingConfig, HealthCheckConfig, 
                   PrometheusConfig, JaegerConfig, BatchConfig, LogOutputConfig, 
                   StructuredLogConfig};

/// 监控配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MonitoringConfig {
    /// 指标配置
    pub metrics: MetricsConfig,
    
    /// 追踪配置
    pub tracing: TracingConfig,
    
    /// 日志配置
    pub logging: LoggingConfig,
    
    /// 健康检查配置
    pub health_check: HealthCheckConfig,
}

impl Default for MonitoringConfig {
    fn default() -> Self {
        Self {
            metrics: MetricsConfig::default(),
            tracing: TracingConfig::default(),
            logging: LoggingConfig::default(),
            health_check: HealthCheckConfig::default(),
        }
    }
}

/// 指标配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricsConfig {
    /// 是否启用指标收集
    pub enabled: bool,
    
    /// 指标端口
    pub port: u16,
    
    /// 指标路径
    pub path: String,
    
    /// Prometheus配置
    pub prometheus: PrometheusConfig,
}

impl Default for MetricsConfig {
    fn default() -> Self {
        // 注意：这里使用默认值，实际项目中应该从crate::defaults导入
        Self {
            enabled: true,
            port: 9090,
            path: "/metrics".to_string(),
            prometheus: PrometheusConfig::default(),
        }
    }
}

/// Prometheus配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrometheusConfig {
    /// 是否启用Prometheus导出
    pub enabled: bool,
    
    /// 收集间隔（秒）
    pub scrape_interval_seconds: u64,
    
    /// 标签
    pub labels: HashMap<String, String>,
}

impl Default for PrometheusConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            scrape_interval_seconds: 15,
            labels: HashMap::new(),
        }
    }
}

/// 追踪配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TracingConfig {
    /// 是否启用追踪
    pub enabled: bool,
    
    /// 采样率（0.0-1.0）
    pub sampling_rate: f64,
    
    /// Jaeger配置
    pub jaeger: Option<JaegerConfig>,
    
    /// 批处理配置
    pub batch: BatchConfig,
}

impl Default for TracingConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            sampling_rate: 1.0,
            jaeger: None,
            batch: BatchConfig::default(),
        }
    }
}

/// Jaeger配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JaegerConfig {
    /// Jaeger代理地址
    pub agent_endpoint: String,
    
    /// 服务名称
    pub service_name: String,
}

/// 批处理配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchConfig {
    /// 批处理大小
    pub batch_size: usize,
    
    /// 批处理超时（毫秒）
    pub timeout_ms: u64,
}

impl Default for BatchConfig {
    fn default() -> Self {
        Self {
            batch_size: 100,
            timeout_ms: 5000,
        }
    }
}

/// 日志配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggingConfig {
    /// 日志级别
    pub level: String,
    
    /// 输出格式（json、text）
    pub format: String,
    
    /// 输出目标
    pub output: Vec<LogOutputConfig>,
    
    /// 结构化日志配置
    pub structured: StructuredLogConfig,
}

impl Default for LoggingConfig {
    fn default() -> Self {
        // 注意：这里使用默认值，实际项目中应该从crate::defaults导入
        Self {
            level: "info".to_string(),
            format: "text".to_string(),
            output: vec![LogOutputConfig::Console],
            structured: StructuredLogConfig::default(),
        }
    }
}

/// 日志输出配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LogOutputConfig {
    /// 控制台输出
    Console,
    /// 文件输出
    File { path: String, rotation: Option<String> },
    /// HTTP输出
    Http { endpoint: String },
}

/// 结构化日志配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StructuredLogConfig {
    /// 是否启用结构化日志
    pub enabled: bool,
    
    /// 最大缓冲区大小
    pub buffer_size: usize,
    
    /// 刷新间隔（毫秒）
    pub flush_interval_ms: u64,
}

impl Default for StructuredLogConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            buffer_size: 1000,
            flush_interval_ms: 1000,
        }
    }
}

/// 健康检查配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthCheckConfig {
    /// 是否启用健康检查
    pub enabled: bool,
    
    /// 健康检查间隔（秒）
    pub interval_seconds: u64,
    
    /// 超时时间（秒）
    pub timeout_seconds: u64,
    
    /// 健康检查路径
    pub path: String,
    
    /// 检查类型（http、tcp、grpc）
    pub check_type: String,
}

impl Default for HealthCheckConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            interval_seconds: 30,
            timeout_seconds: 5,
            path: "/health".to_string(),
            check_type: "http".to_string(),
        }
    }
}